package com.liuqi.knet.rpc.cluster.loadbalance;

import com.liuqi.knet.rpc.cluster.ClusterConfig;
import com.liuqi.knet.rpc.cluster.LoadBalanceNode;
import java.util.ArrayList;
import java.util.List;

/**
 * 权重
 *
 * @author liuqi
 * @date 2024/6/20 10:45
 **/
public class WeightLoadBalance extends RoundLoadBalance {

    public WeightLoadBalance(ClusterConfig clusterConfig) {
        super(clusterConfig);
        List<LoadBalanceNode.Node> nodes = getLoadBalanceNode().getNodes();
        List<LoadBalanceNode.Node> newNodes = new ArrayList<LoadBalanceNode.Node>();
        for (LoadBalanceNode.Node node : nodes) {
            for (int i = 0; i < node.getWeight(); i++) {
                newNodes.add(new LoadBalanceNode.Node(node.getRemote(), node.getWeight()));
            }
        }
        getLoadBalanceNode().setNodes(newNodes);
    }

}
